package com.sky.service.impl;

import com.sky.context.UserHolder;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {


    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    //加入购物车
    @Override
    public void save(ShoppingCart shoppingCart) {
        //1.先根据用户id+套餐/菜品从购物车表查询
        Long userId = UserHolder.get();//通过ThreadLocal获取用户ID
        shoppingCart.setUserId(userId);//设置用户id
        //1-1.查询数据库
        ShoppingCart shoppingCartFromDB = shoppingCartMapper.findOne(shoppingCart);
        //2.如果查询不到任何东西,需要新增一条记录
        if (shoppingCartFromDB != null) {
            //2.如果查询到了,让number(份数)+1就行了,在更新回去
            shoppingCartFromDB.setNumber(shoppingCartFromDB.getNumber() + 1);
            shoppingCartMapper.update(shoppingCartFromDB);
        } else {
            //3.需要新增一条记录
            if (shoppingCart.getDishId() != null) {
                //3-1 注意这里是用菜品的id去菜品表查数据补齐购物车需要的菜品表部分
                DishVO dishVO = dishMapper.findById(shoppingCart.getDishId());//注意这是菜品的id
                //3-2 菜品补齐数据
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dishVO.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                //3-3 保存
                shoppingCartMapper.save(shoppingCart);
            } else {
                //4-1 注意这里是用套餐的id去套餐表查数据补齐购物车需要的套餐表部分
                SetmealVO setmealVO = setmealMapper.findById(shoppingCart.getSetmealId());
                //4-2 套餐补齐数据
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                //4-3 保存
                shoppingCartMapper.save(shoppingCart);
            }
        }
    }

    //查询购物车
    @Override
    public List<ShoppingCart> findList() {
        //1.获取登陆人id
        Long userId = UserHolder.get();
        return shoppingCartMapper.findByUserId(userId);
    }

    //清空购物车
    @Override
    public void clean() {
        //1.获取登陆人id
        Long userId = UserHolder.get();
        shoppingCartMapper.deleteByUserId(userId);
    }

    //删减购物车中的一个商品
    @Override
    public void sub(ShoppingCart shoppingCart) {
        //1.获取登陆人id
        Long userId = UserHolder.get();
        shoppingCart.setUserId(userId);
        //2.根据用户id查询购物车
        ShoppingCart shoppingCartFromDB = shoppingCartMapper.findOne(shoppingCart);
        if (shoppingCartFromDB.getNumber() == 1) {
            //2-1 如果数量为1,直接删除
            shoppingCartMapper.delete(shoppingCart);
        }else {
            //2-2 如果数量大于1,数量减1,更新回去
            shoppingCartMapper.update(shoppingCartFromDB);
            shoppingCartFromDB.setNumber(shoppingCartFromDB.getNumber() - 1);
            //2-3 更新回去
            shoppingCartMapper.update(shoppingCartFromDB);
        }

    }


}

